<?php
/**
 * Created by PhpStorm.
 * User: 吕金鹏
 * Date: 2021/5/21
 * Time: 11:35 上午
 */

namespace app\index\controller;
use think\Db;
use think\facade\Request;

class Ordertimes extends Base
{
    public function index(){
        $id = input("id");
        $Tlist = Db::name("inorder_times")->where("oid",$id)->select();
        $this->assign('list',$Tlist);
        $this->assign("oid",$id);
        return $this->fetch();
    }
    public function addorder(){
        $id = input("id");
        if(Request::isPost()){
            $data = input("post.");
            $data['time'] = time();
            $data["admin_name"] = $this->admin["rel_name"];
            $data['admin_id'] = $this->admin['id'];
            $data['status'] = 1;
            $data['c_name'] = Db::name("gongying")->where("id",$data['cid'])->field("name")->find()['name'];
            $res = Db::name("inorder_times")->insert($data);
            if($res){
                return json(['code'=>1,'msg'=>'添加成功']);
            }
            return json(['code'=>0,'msg'=>'网络延时,稍后再试']);
        }
        $this->assign("id",$id);
        //供应商
        $gongying = Db::name("gongying")->where("status",1)->where("if_del",0)->select();
        $this->assign("gongying",$gongying);
        return $this->fetch();
    }
    public function changegoods(){
        if(Request::isPost()){
            $data = input("post.");
            //存入批次单
            $goods = [];
            foreach($data['gid'] as $key=>$val){
                $goods[$key]['gid'] = $val;
                $goods[$key]['num'] = $data['num'][$key];
                $goods[$key]['ware'] = $data['ware'][$key];
            }

            $goods = json_encode($goods);
//            dump($goods);die;
            $res = Db::name("inorder_times")->where('id',$data['id'])->update(['goods'=>$goods]);
            if($res){
                return json(['code'=>1,'msg'=>"添加成功"]);
            }
            return json(['code'=>0,"msg"=>"网络延时,稍后再试"]);
        }
        $oid = input("oid");
        $id = input('id');
        //查看入库单商品
        $order = Db::name("procure")->where("id",$oid)->find();
        $goods_list = json_decode($order['goods'],true);
        $goods = [];
        $info = Db::name("inorder_times")->where("id",$id)->field("goods")->find();
        if(!$info['goods']){
            $order = Db::name("inorder_times")->where("oid",$oid)->field("goods")->select();
            $goods_ary = [];
            //整理已经入库的商品
            foreach($order as $k=>$v){
                if($v['goods']){
                    $new_v = json_decode($v['goods'],true);
                    foreach($new_v as $vv){
                        if(isset($goods_ary[$vv['gid']])){
                            $goods_ary[$vv['gid']] += $vv['num'];
                        }else{
                            $goods_ary[$vv['gid']] = $vv['num'];
                        }
                    }
                }
            }
            foreach($goods_list as $key=>$val){
                $goods[$key] = Db::name("goods")->where("id",$val['gid'])->find();
                $goods[$key]['ware_id'] = $val['ware'];
                $goods[$key]['ware'] = Db::name("warehouse")->where("id",$val['ware'])->find()['name'];
                $goods[$key]['num'] = $val['num'];
                //减掉已经生成批次的商品
                foreach($goods_ary as $k=>$v){
                    if($val['gid']==$k){
                        $goods[$key]['num'] -= $v;
                    }
                }
            }
        }else{
            $goods = json_decode($info['goods'],true);
            foreach($goods as $key=>$val){
                $goods[$key] = Db::name("goods")->where("id",$val['gid'])->find();
                $goods[$key]['ware_id'] = $val['ware'];
                $goods[$key]['ware'] = Db::name("warehouse")->where("id",$val['ware'])->find()['name'];
                $goods[$key]['num'] = $val['num'];

            }
        }

        $this->assign('goods',$goods);
        $this->assign('id',$id);
        $this->assign("oid",$oid);
        return $this->fetch();
    }
    public function changeOrder(){
        $id = input("id");
        $status = input("status");
        $res = Db::name("inorder_times")->where("id",$id)->update(['status'=>$status,'send_time'=>time()]);
        //所有商品的[ICI单确认后,修改采购单状态
        $info = Db::name("inorder_times")->where("id",$id)->find();
        $oorder = Db::name("procure")->where("id",$info['oid'])->find();
        $order = Db::name("inorder_times")->where("oid",$info['oid'])->select();

        $goods_ary = [];
        //整理已经入库的商品
        foreach($order as $k=>$v){
            if($v['goods']){
                $new_v = json_decode($v['goods'],true);
                foreach($new_v as $vv){
                    if(isset($goods_ary[$vv['gid']])){
//                        echo $goods_ary[$vv['gid']];
                        $goods_ary[$vv['gid']] += $vv['num'];
                    }else{
                        $goods_ary[$vv['gid']] = $vv['num'];
                    }
                }
            }
        }
        $goods = json_decode($oorder['goods'],true);
        $if_finish = 1;

        foreach($goods as $key =>$val){
            foreach($goods_ary as $k=>$v){
                if($val['gid']==$k){

                   if($goods[$key]['num'] != $v) $if_finish = 0;
                }
            }
        }
//        echo $if_finish;die;
        if($if_finish==1){
            Db::name("procure")->where("id",$info['oid'])->update(['status'=>4]);
        }
        if($res){
            return json(['code'=>1,'msg'=>'变更成功']);
        }
        return json(['code'=>2,'msg'=>'网络延时,稍后再试']);
    }
    //入库单
    public function inorder(){
        $times_id = input("times_id");


    }
}